<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\WaterShop;

class WaterShopController extends Controller
{
    protected $message = [
        'name.required' => '商品名称必须',
        'name.max' => '商品名称不能超过100个字符',
        
        'water_category_id.required' => '分类必须',
        'water_category_id.integer' => '分类格式为整数值',
        'water_category_id.exists' => '分类不存在',
        
    ];
    /**
     * 商品列表
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $perPage = $request->input('pageSize',15);
        $name = $request->input('name', '');
        $query = WaterShop::query();
        if(!empty($name)) {
            $query->where('name', 'like', $name.'%');
        }
        $cid = $request->input('cid', '');
        if(!empty($cid)) {
            $query->where('company_id', $cid);
        }
        $sid = $request->input('sid', '');
        if(!empty($cid)) {
            $query->where('site_id', $sid);
        }
        
        $created = $request->input('created_at', []);
        if(!empty($created)) {
            $query->whereBetween('created_at', $created);
        }
        
        $query->with([
            'category' => function($query) {
                $query->select(['id', 'name']);
            },
        ]);

        $list = $query->paginate($perPage);
        return $list;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * 添加商品.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // $data = $request->input();

        $data = $this->validateData($request);

        $ret = WaterShop::create($data);
        if(!$ret){
            $this->error('保存失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 更新商品.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        // $data = $request->input();

        $data = $this->validateData($request, $id);
        
        $ws = WaterShop::find($id);
        
        
        $ret = $ws->update($data);
        if(!$ret){
            $this->error('保存失败');
        }
    }
    
    //验证表单
    protected function validateData($request,$id = false) {

        $rules = [
            'name' => 'required|max:100',
            'water_category_id' => 'required|integer|exists:water_categories,id',
        ];


        $v = $this->validate2($request, $rules, $this->message);
        $user = $request->user();
       
        $data['creater_uid'] = $user->userid;
        $data['creater_name']= $user->username;
        
       $v->validate();
       
       return $data;
    }

    /**
     * 删除商品
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $ws = WaterShop::findOrFail($id);
        if(!$ws->delete()){
            $this->error('删除失败');
        }
    }
}
